package org.game.util;

import org.yaml.snakeyaml.Yaml;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Map;

public class YamlUtil {
    public static Map<String, Object> read(Class clazz) throws FileNotFoundException {
        String[] strings = clazz.getPackageName().split("\\.");
        String path = strings[strings.length - 1] + "/src/main/resources/application.yml";

        Yaml yaml = new Yaml();

        return yaml.loadAs(new FileInputStream(path), Map.class);
    }

    public static Map<String, Object> read(Class clazz, int layer) throws FileNotFoundException {
        String[] strings = clazz.getPackageName().split("\\.");
        String preStr = "";
        for (int i = layer; i > 0; i--) {
            preStr += strings[strings.length - i] + "/";
        }
        String path = preStr + "src/main/resources/application.yml";

        Yaml yaml = new Yaml();

         return yaml.loadAs(new FileInputStream(path), Map.class);
    }

    public static Map<String, Object> read(String path) throws FileNotFoundException {
        Yaml yaml = new Yaml();

        return yaml.loadAs(new FileInputStream(path), Map.class);
    }
}
